/*
 * @lc app=leetcode.cn id=20 lang=javascript
 *
 * [20] 有效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  const matchMap = new Map([
    ["(", ")"],
    ["{", "}"],
    ["[", "]"],
  ]);
  const stack = [];
  for (let i = 0; i < s.length; i++) {
    // 左括号
    if (matchMap.has(s[i])) stack.push(s[i]);
    // 右括号
    else {
      let ch = stack.pop();
      if (matchMap.get(ch) !== s[i]) return false;
    }
  }
  return stack.length === 0;
};
// @lc code=end
